const multer = require('@koa/multer');
const { AVATAR_UPLOAD_PATH } = require('../config/path');

const upload = multer({
  storage: multer.diskStorage({
    destination(req, file, cb) {
      cb(null, AVATAR_UPLOAD_PATH); //指定存储文件夹为 imgs，相对路径相对于启动路径
    },
    filename(req, file, cb) {
      // 解决中文名乱码的问题
      file.originalname = Buffer.from(file.originalname, "latin1").toString(
        "utf8"
      );
      cb(null, Date.now() + "_" + file.originalname);
    },
  }),
});

// fieldname:photo
let handleUploadSingle = upload.single("photo");
let handleUploadMulti = upload.array("photos");

module.exports = {
  handleUploadSingle,
  handleUploadMulti
}